// Beginning Game Programming
// Chapter 9
// dxaudio.cpp - DirectSound framework header file

#include "CMusic.h"


CMusic::CMusic()
{

}

CMusic::~CMusic()
{
	delete sound_bounce;
}

int CMusic::InitSound(HWND hwnd)
{
	//initialize DirectSound
    if (!Init_DirectSound(hwnd))
    {
        MessageBox(hwnd, "Error initializing DirectSound", "Error", MB_OK);
        return 0;
    }
	//load bounce wave file
    sound_bounce = LoadSound("s.wav");
    if (sound_bounce == NULL)
        return 0;
}

int CMusic::Init_DirectSound(HWND hwnd)
{
    HRESULT result;

    //create DirectSound manager object
    dsound = new CSoundManager();

    //initialize DirectSound
    result = dsound->Initialize(hwnd, DSSCL_PRIORITY);
    if (result != DS_OK)
        return 0;

    //set the primary buffer format
    result = dsound->SetPrimaryBufferFormat(2, 22050, 16);
    if (result != DS_OK)
        return 0;

    //return success
    return 1;
}

CSound* CMusic::LoadSound(char *filename)
{
    HRESULT result;

    //create local reference to wave data
    CSound *wave;

    //attempt to load the wave file
    result = dsound->Create(&wave, filename);
    if (result != DS_OK)
        return NULL;

    //return the wave
    return wave;
}

void CMusic::PlaySound()
{
    sound_bounce->Play();
}

void CMusic::LoopSound()
{
    sound_bounce->Play(0, DSBPLAY_LOOPING);
}

void CMusic::StopSound()
{
    sound_bounce->Reset();
	sound_bounce->Stop();
}
